package cn.zwz.customer.controller;

import cn.hutool.core.util.IdUtil;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.customer.entity.Supplier;
import cn.zwz.customer.service.ISupplierService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 熊英杰
 */
@Slf4j
@RestController
@Api(tags = "供应商信息")
@RequestMapping("/zwz/supplier")
@Transactional
public class SupplierController {

    @Autowired
    private ISupplierService iSupplierService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条供应商信息")
    public Result<Supplier> get(@RequestParam String id){
        return new ResultUtil<Supplier>().setData(iSupplierService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部供应商信息个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iSupplierService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部供应商信息")
    public Result<List<Supplier>> getAll(){
        return new ResultUtil<List<Supplier>>().setData(iSupplierService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询供应商信息")
    public Result<IPage<Supplier>> getByPage(@ModelAttribute Supplier supplier ,@ModelAttribute PageVo page){
        QueryWrapper<Supplier> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(supplier.getName())) {
            qw.like("name",supplier.getName());
        }
        if(!ZwzNullUtils.isNull(supplier.getAddress())) {
            qw.like("address",supplier.getAddress());
        }
        if(!ZwzNullUtils.isNull(supplier.getMobile())) {
            qw.like("mobile",supplier.getMobile());
        }
        IPage<Supplier> data = iSupplierService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Supplier>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改供应商信息")
    public Result<Supplier> saveOrUpdate(Supplier supplier){
        if(iSupplierService.saveOrUpdate(supplier)){
            return new ResultUtil<Supplier>().setData(supplier);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增供应商信息")
    public Result<Supplier> insert(Supplier supplier){
        supplier.setCode(IdUtil.getSnowflake(1, 1).nextId() + "");
        iSupplierService.saveOrUpdate(supplier);
        return new ResultUtil<Supplier>().setData(supplier);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑供应商信息")
    public Result<Supplier> update(Supplier supplier){
        iSupplierService.saveOrUpdate(supplier);
        return new ResultUtil<Supplier>().setData(supplier);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除供应商信息")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iSupplierService.removeById(id);
        }
        return ResultUtil.success();
    }
}
